/**
 * 基础图形绘制交互
 * 矩形/圆形/线条...等等
 */

import namespace from "../../namespace";

class BasisShapePolicy extends namespace.policy.canvas.UniquePolicy {
  constructor() {
    super();

    this.startDraw = false;

    // 绘制图形
    this.shape = null;

    this.x = null;
    this.y = null;
  }

  onMouseDown(canvas, x, y) {
    this.startDraw = true;
    this.x = x;
    this.y = y;

    this.createFeakeShape(x, y);
  }

  onMouseDrag(canvas, dx, dy) {
    let abs = Math.abs;
    this.shape.setDimension(abs(dx), abs(dy));
    this.shape.setPosition(this.x + Math.min(0, dx), this.y + Math.min(0, dy));
  }

  onMouseUp() {
    this.startDraw = false;
    this.x = 0;
    this.y = 0;

    let command = new namespace.command.CommandAdd(
      this.canvas,
      this.shape.clone()
    );

    this.canvas.commandStack.execute(command);

    this.shape.hide();
    this.shape = null;
  }

  onPressEscKeyboard() { }
}

namespace.policy.canvas.BasisShapePolicy = BasisShapePolicy;
